package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClaimBill;
import cn.wolfcode.crm.query.CompensationQueryObject;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.service.IClaimBillService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.List;

@Controller
@RequestMapping("compensation")
public class CompensationController {
    @Autowired
    private IAccidentService service;
    @Autowired
    private IClaimBillService claimBillService;

    @RequestMapping("")
    @RequiresPermissions("compensation:index")
    @PermissionName("事故赔损表首页")
    private String index(){
        return "compensation/list";
    }


    /*
     * 查询审核成功并需要支付及支付成功的数据
     * */
    @RequestMapping("listByCompensationForPay")
    @ResponseBody
    private PageResult listByCompensationForPay(CompensationQueryObject qo){
        return  claimBillService.query(qo);
    }
    /*
     * 更改理赔明细表中状态
     * */
    @RequestMapping("changeStateAudited")
    @ResponseBody
    private JsonResult changeStateAudited(Long id){
        JsonResult result = new JsonResult();
        try {
            service.changeStateAudited(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("提交验证失败");
        }
        return result;
    }
    /*
     * 废弃数据
     * */
    @RequestMapping("delete")
    @ResponseBody
    private JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("提交验证失败");
        }
        return result;
    }
    /*
     * 导出数据
     * */
    @RequestMapping("report")
    @ResponseBody
    private JsonResult report(Long id){
        JsonResult result = new JsonResult();
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/理赔明细.xls"));
            WritableSheet sheet = workbook.createSheet("理赔明细", 0);
            String[] titles = new String[]{"编号","受益人","账户","理赔金额","保险单号"};
            Label cell;
            for(int i = 0 ;i<titles.length;i++){
                cell = new Label(i,0,titles[i]);
                sheet.addCell(cell);
            }
            List<ClaimBill> bills = claimBillService.getAll();
            for(int i=1;i<=bills.size();i++){
                Number no = new Number(0,i,bills.get(i-1).getId());
                sheet.addCell(no);
                cell = new Label(1,i,bills.get(i-1).getBeneficiary());
                sheet.addCell(cell);
                cell = new Label(2,i,bills.get(i-1).getAccount());
                sheet.addCell(cell);
                no = new Number(3, i, bills.get(i - 1).getAmountpay());
                sheet.addCell(no);
                cell = new Label(4,i,bills.get(i-1).getInsuranceNo());
                sheet.addCell(cell);
            }
            workbook.write();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("导出数据失败");
        }
        return result;
    }

}
